package com.example.yls.demoa;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by yls on 2018/10/22.
 */

class StudentAdapter extends RecyclerView.Adapter<StudentViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;

    public StudentAdapter(Context context, ArrayList<Student> studentArrayList) {
        this.context = context;
        this.studentArrayList = studentArrayList;
    }

    public void changeData(ArrayList<Student> arrayList) {
        this.studentArrayList = arrayList;
        notifyDataSetChanged();
    }

    @Override
    public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item,parent,false);

        StudentViewHolder viewHolder = new StudentViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(StudentViewHolder holder, int position) {
        final Student s = studentArrayList.get(position);

        holder.imgID.setImageResource(s.getImgID());
        holder.name.setText(s.getName());
        holder.age.setText(String.valueOf(s.getAge()));

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();

                intent.setClass(context,EditActivity.class);
                context.startActivity(intent);



                Toast.makeText(context,s.getName()+"    "+s.getAge(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
}
